<!DOCTYPE html>
<html>
    <head>
        <title locale="title/title">Module List</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
        <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
        <script type="text/javascript" src="../../script/jquery.min.js"></script>
        <script type="text/javascript" src="../../script/semantic/semantic.min.js"></script>
        <!-- <script type="text/javascript" src="../../script/ao_module.js"></script> -->
    </head>
    <body>
        <div class="ui container">
            <div style="width: 100%; overflow-y: auto;">
                <table class="ui basic celled structured unstackable table">
                    <thead>
                    <tr>
                        <th rowspan="2" locale="table/module">Module</th>
                        <th rowspan="2" locale="table/group">Group</th>
                        <th rowspan="2" locale="table/ext">Supported Extensions</th>
                        <th colspan="3" locale="table/modes">Supported Mode</th>
                    </tr>
                    <tr>
                        <th locale="table/modes/default">Default</th>
                        <th locale="table/modes/floatwindow">FloatWindow</th>
                        <th locale="table/modes/embedded">Embedded</th>
                    </tr>
                    </thead>
                    <tbody id="moduleList">
                    
                    </tbody>
                </table>
            </div>
              <div class="ui divider"></div>
              <p locale="desc/reload">If you have installed WebApps manually, you can click the "Reload WebApps" button to load it without restarting ArozOS.</p>
              <button id="reloadWebappButton" class="ui basic small blue button" onclick="reloadWebapps();">
                <i class="refresh icon"></i> <span locale="button/reload">Reload WebApps</span>
              </button>
              <br><br>
        </div>
        <script>

            //Applocale
            var moduleListLocale = NewAppLocale();
            moduleListLocale.init("../locale/system_settings/modulelist.json", function(){
                moduleListLocale.translate();
                initModuleList();
            });

            

            function reloadWebapps(){
                let moduleListBackup = $("#moduleList").html();
                $("#moduleList").html(`<tr><td colspan="6"><i class="ui loading spinner icon"></i> Reloading...</tr></td>`);
                $.ajax({
                   url: "../../system/modules/reload", 
                   success: function(data){
                        initModuleList();
                   },
                   error: function(){
                       //Reload failed (Permission denied?)
                       $("#moduleList").html(moduleListBackup);
                       $("#reloadWebappButton").addClass("disabled").html("<i class='ui remove icon'></i> No Permission");
                   }    
                });
            }

            function initModuleList(){
                $("#moduleList").html("");
                $.ajax({
                    url: "../../system/modules/list",
                    success: function(data){
                        console.log(data);
                        for (var i =0; i < data.length; i++){
                            var thisModule = data[i];
                            var iconURL = "../../img/system/service.png";
                            if (thisModule.IconPath !== ""){
                                iconURL = "../../" + thisModule.IconPath;
                            }
                            var supportMode = ['<i class="large red remove icon"></i>', '<i class="large red remove icon"></i>', '<i class="large red remove icon"></i>'];
                            var group = "Unknown";
                            if (thisModule.Group != ""){
                                group = thisModule.Group;
                            }
                            if (thisModule.StartDir != ""){
                                supportMode[0] = '<i class="large green checkmark icon"></i>';
                            }
                            if (thisModule.SupportFW == true){
                                supportMode[1] = '<i class="large green checkmark icon"></i>';
                            }
                            if (thisModule.SupportEmb == true){
                                supportMode[2] = '<i class="large green checkmark icon"></i>';
                            }
                            var supportedExt = "N/A";
                            if (thisModule.SupportedExt !== null){
                                supportedExt = thisModule.SupportedExt.join("<br>");
                            }
                            $("#moduleList").append(`<tr>
                                <td>
                                    <h4 class="ui image header" style="white-space: nowrap;">
                                        <img src="${iconURL}" class="ui mini rounded image">
                                        <div class="content">
                                            ${thisModule.Name}
                                            <div class="sub header">${moduleListLocale.getString("item/version", "Version:")} ${thisModule.Version} </div>
                                        </div>
                                    </h4>
                                </td>
                                <td>
                                ${group}
                                </td>
                                <td>
                                ${supportedExt}
                                </td>
                                <td>
                                ${supportMode[0]}
                                </td>
                                <td>
                                ${supportMode[1]}
                                </td>
                                <td>
                                ${supportMode[2]}
                                </td>
                            </tr>`);
                        }
                    }
                });
            }

            
            
        </script>
    </body>
</html>